#include "ct_config.h"

#include "ct_sys.h"

/*****************************************************************************
 * Trace Definitions
 ****************************************************************************/
volatile uint8_t g_tickTime_HiByte = 0;    // 高8位
volatile uint8_t g_tickTime_LoByte = 0;    // 低8位

/*****************************************************************************
 * Pre-processor Definitions
 ****************************************************************************/

void ct_EnableAllIntt(ct_bool bEnable) {
    EA = bEnable;
}

ct_bool ct_ReadGlobalInttStatus(void) {
    return EA;
}

/*****************************************************************************
 * Public Functions
 ****************************************************************************/
/**
 * @brief   Initialize the tick module.
 * @param   ms The tick interval in milliseconds.
 * @return  None
 */
void ct_delay(unsigned int ms) {
    unsigned int i, j;
    unsigned int fosc = 750;
    for (i = 0; i < ms; i++) {
        // 24MHz,1ms
        for (j = 0; j < fosc; j++)
            ;
    }
}

/**
 * @brief   Initialize the tick module2.
 * @param   us The tick interval in milliseconds.
 * @note    fosc= 32=>50us; 70=>100us
 * @return  None
 */
void ct_delay_us(unsigned int us) {
    unsigned int i, j;
    unsigned int fosc = 70;    // 32;
    for (i = 0; i < us; i++) {
        // 24MHz,32:50us 70:100us
        for (j = 0; j < fosc; j++)
            ;
    }
}

/**
 * @brief   Increase the tick count.
 * @param   None
 * @return  None
 */
void ct_IncTick(void) {
    if (++g_tickTime_LoByte == 0) {
        g_tickTime_HiByte++;
    }
}

/**
 * @brief   Get the current tick count.
 * @param   None
 * @return  The current tick count.
 */
unsigned int ct_GetTick(void) {
    uint8_t high, low;
    do {
        high = g_tickTime_HiByte;
        low  = g_tickTime_LoByte;
    } while (high != g_tickTime_HiByte);

    return ((unsigned int)high << 8) | low;
}

/**
 * @brief This API configures modify system HIRC value
 * @param[in] u8FsysMode . Valid values are:
 *                       - \ref HIRC_24                 :Internal HIRC 24MHz .
 *                       - \ref HIRC_16                 :Internal HIRC 16MHz.
 *                       - \ref HIRC_166                :Internal HIRC 16.6MHz.
 * @note      None.
 * @exmaple : MODIFY_HIRC(HIRC_24);
 */
void ct_sys_ModifyHIRC(void) {
#if CT_SYSCLK == 24000000UL
    MODIFY_HIRC(CT_HIRC_24);
#elif CT_SYSCLK == 16000000UL
    MODIFY_HIRC(CT_HIRC_16);
#elif CT_SYSCLK == 16600000UL
    MODIFY_HIRC(CT_HIRC_166);
#endif
}

/**
 * @brief Clock switch function
 * @param[in] fsysmode Decides the clock source. Valid values are:
 *                      - \ref CT_FSYS_HXT
 *                     - \ref CT_FSYS_LXT
 *                    - \ref CT_FSYS_HIRC
 *                   - \ref CT_FSYS_LIRC
 *                 - \ref CT_FSYS_PLL
 * @return None
 * @details This function is used to switch system clock source.
 * @note This function is only supported in ML51 series MCU.
 */
void ct_sys_ClkSelect(ct_fsys_mode_t fsysmode) {
    FsysSelect(fsysmode);
}

/**
 * @brief Clock enable function
 * @param[in] fsysmode Decides the clock source. Valid values are:
 *                     - \ref CT_FSYS_HXT
 *                   - \ref CT_FSYS_LXT
 *                 - \ref CT_FSYS_HIRC
 *              - \ref CT_FSYS_LIRC
 * @return None
 */
void ct_sys_ClkEnable(ct_fsys_mode_t fsysmode) {
    ClockEnable(fsysmode);
}

/**
 * @brief Clock disable function
 * @param[in] fsysmode Decides the clock source. Valid values are:
 *                     - \ref CT_FSYS_HXT
 *                   - \ref CT_FSYS_LXT
 *                 - \ref CT_FSYS_HIRC
 *              - \ref CT_FSYS_LIRC
 * @return None
 */
void ct_sys_ClkDisable(ct_fsys_mode_t fsysmode) {
    ClockDisable(fsysmode);
}

/**
 * @brief   Software reset API
 * @param   bEnable in APROM/LDROM after reset
 *          - \ref BOOT_APROM: Run in APROM after reset .
 *          - \ref BOOT_LDROM: Run in LDROM after reset.
 * @return None
 * @details   None
 */
void ct_sys_SoftReset(void) {
#ifdef _USE_BOOTLOADER_
    Software_Reset(BOOT_LDROM);
#else
    Software_Reset(BOOT_APROM);
#endif
}

/**
 * @brief       MCU power down mode enable API
 * @param       Enable power down mode
 *                       - \ref ENABLE: Run into power down mode .
 * @return      None
 * @details     None
 */
void ct_sys_PowerDown(ct_bool bEnable) {
    PowerDown_Mode(bEnable);
}

/**
 * @brief       MCU idle mode enable API
 * @param       Enable idle mode
 *                       - \ref ENABLE: Run into idle mode .
 * @return      None
 * @details     None
 */
void ct_sys_Idle(ct_bool bEnable) {
    Idle_Mode(bEnable);
}

/**
 * @brief Watchdog timer enable function
 * @return None
 * @details This function is used to enable watchdog timer.
 */
void ct_wdt_Enable(void) {
    WDT_Open(256);
    WDT_Interrupt(Disable);
}

/**
 * @brief Watchdog timer disable function
 * @return None
 * @details This function is used to disable watchdog timer.
 */
void ct_wdt_Disable(void) {
    clr_WDCON_WDTR;
}